import ToolActionApi from "@/tool/http/tool-action.js";
import toolListenCache from "../data_listen/tool-listen-cache";

// let dataSourceAction = ToolActionApi.init("data_source");
let dataListenAction = ToolActionApi.init("data_listen");
let channelAction = ToolActionApi.init("function_channel_business");
let productAction = ToolActionApi.init("things_product");
let deviceAction = ToolActionApi.init("things_device");

// 数据监听 - 字典数据
let dataListenList = [];
let dataListenDict = [];
let channelList = [];
let channelDict = [];
// 数据规则字典
let dictRule = [];

let extendArrays = [
  {
    name: "triggerArr",
    memo: "触发器配置",
    arrs: [
      {
        name: "triggerLogic",
        memo: "触发逻辑关系",
        is: (_, index) => index !== 0,
        dictListFun: async () => await toolListenCache.dictLogic(),
      },
      {
        name: "listenDataId",
        memo: "数据监听器",
        dictListFun: async () => dataListenDict,
      },
      {
        name: "listenDataKey",
        memo: "数据触发规则列",
        dictListFun: async (watchForm) => {
          if (!watchForm.listenDataId) {
            return [];
          }
          let dataListen = dataListenList.find(
            (item) => item.id === watchForm.listenDataId
          );
          return await toolListenCache.dictTableCol(
            dataListen.extend_json.listenDataSourceId,
            dataListen.extend_json.listenTableName
          );
        },
      },
      {
        name: "listenDataRule",
        memo: "数据触发规则",
        dictListFun: async () => dictRule,
      },
      {
        name: "listenDataVal",
        memo: "数据触发规则值",
      },
    ],
  },
  {
    name: "exceArr",
    memo: "执行器配置",
    arrs: [
      {
        name: "channelId",
        memo: "渠道商",
        dictListFun: async () => channelDict,
      },
      {
        name: "productId",
        memo: "产品",
        dictListFun: async (watchForm) => {
          if (!watchForm.channelId) {
            return [];
          }
          return await productAction.dict(undefined, undefined, {
            rules: { channel_id__eq: watchForm.channelId },
          });
        },
      },
      {
        name: "deviceId",
        memo: "设备",
        dictListFun: async (watchForm) => {
          if (!watchForm.productId) {
            return [];
          }
          return await deviceAction.dict(undefined, undefined, {
            rules: { product_id__eq: watchForm.productId },
          });
        },
      },
      {
        name: "action",
        memo: "动作",
        dictListFun: async (watchForm) => {
          if (!watchForm.channelId) {
            return [];
          }
          let channelObj = channelList.find(
            (item) => item.id === watchForm.channelId
          );
          return (
            await ToolActionApi.list({
              platformType: channelObj.enum_platform_type,
            })
          ).map((item) => ({ name: item.name, val: item.actionCode }));
        },
      },
    ],
  },
];

const rule = {
  queryParams: [],
  extendArrays,
  metaConfig: {
    id: { name: "id" },
  },
};

export default async function () {
  (async function () {
    dataListenList = await dataListenAction.list();
    dataListenDict = dataListenList.map((item) => ({
      name: item.name,
      val: item.id,
    }));
    dictRule = await ToolActionApi.enumDict("data_rule");
  })();
  channelList = await channelAction.list();
  channelDict = channelList.map((item) => ({
    name: item.name,
    val: item.id,
  }));
  return rule;
}
